// geo location

var geo = Ext.create('Ext.util.Geolocation', {
    autoUpdate: true,
    listeners: {
        locationupdate: function(geo) {
           
        },
        locationerror: function(geo, bTimeout, bPermissionDenied, bLocationUnavailable, message) {
            if(bTimeout){
                alert('Timeout occurred.');
            } else {
                alert('Error occurred.');
            }
        }
    }
});


// Populate the database 
//
function populateDB(tx) {
    // Drop tables
    tx.executeSql('DROP TABLE IF EXISTS DICTIONARYSOURCE');
    tx.executeSql('DROP TABLE IF EXISTS DICTIONARYTARGET');
    tx.executeSql('DROP TABLE IF EXISTS SPEAKER');
    tx.executeSql('DROP TABLE IF EXISTS COLLECTOR');
    
    // Create COLLECTOR table and populate
    tx.executeSql('CREATE TABLE IF NOT EXISTS COLLECTOR (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name, dateOfBirth, gender, comment)');
    tx.executeSql('INSERT INTO COLLECTOR (id, name, dateOfBirth, gender, comment) VALUES (1, "Bruce Birch", "1/1/1970", "Male", "first collector")');
    
    //CREATE SPEAKER table and populate
    tx.executeSql('CREATE TABLE IF NOT EXISTS SPEAKER (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name, dateOfBirth, gender, comment)');
    tx.executeSql('INSERT INTO SPEAKER (id, name, dateOfBirth, gender, comment) VALUES (1, "Axsoul Aden", "1/1/1970", "Male", "first speaker")');
    
   
    // Create DICTIONARYTARGET table and populate
    tx.executeSql('CREATE TABLE IF NOT EXISTS DICTIONARYTARGET (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, speakerId, collectorId, targetWord, detailedEntry, audioURL, imageURL, note, savedDate, savedTime, geoLocation, recordingDevice, FOREIGN KEY (speakerId) REFERENCES SPEAKER(id), FOREIGN KEY (collectorId) REFERENCES COLLECTOR(id) )');
    tx.executeSql('INSERT INTO DICTIONARYTARGET (id, speakerId, collectorId, targetWord, detailedEntry, audioURL, imageURL, note) VALUES(1, 1, 1, "riwurrujun", "<p class=BlockParagraph>&#8212; <span class=Partofspeech>v. ang.</span> <span class=Sensenumber>5)&nbsp;</span><span class=DefinitionE>dig grave</span><span class=fstandard>.</span> <span class=Examplev>Kuburr angkanan kunak.</span> <span class=ExamplefreetransE>Tomorrow theyll dig the grave</span></p>", "assets/words/y1.mp3", "/images/image1.jpg", "")');
    tx.executeSql('INSERT INTO DICTIONARYTARGET (id, speakerId, collectorId, targetWord, detailedEntry, audioURL, imageURL, note) VALUES(2, 1, 1, "balarrakan target detail data", "She is telling truth", "", "", "")');
    

    
    // Create DICTIONARYSOURCE table and populate
    tx.executeSql('CREATE TABLE IF NOT EXISTS DICTIONARYSOURCE (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, dictionaryTargetId, sourceWord, partOfSpeech, inflection, disambiguation, targetWord, isEditable, FOREIGN KEY (dictionaryTargetId) REFERENCES DICTIONARYTARGET(id))');
    tx.executeSql('INSERT INTO DICTIONARYSOURCE (id, dictionaryTargetId, sourceWord, partOfSpeech, inflection, disambiguation, targetWord, isEditable) VALUES (1, 1, "foot", "n.", "his/her/its foot", "on stomach", "balarrakanSource", "true")');
    tx.executeSql('INSERT INTO DICTIONARYSOURCE (id, dictionaryTargetId, sourceWord, partOfSpeech, inflection, disambiguation, targetWord, isEditable) VALUES (2, 1, "head", "n.", "his/her/its foot", "on stomach",  "riwurrujun", "true")');
    tx.executeSql('INSERT INTO DICTIONARYSOURCE (id, dictionaryTargetId, sourceWord, partOfSpeech, inflection, disambiguation, targetWord, isEditable) VALUES (3, 2, "hand", "n.", "his/her/its foot", "on stomach", "balarrakan", "false")');
    
       
}





// Transaction error callback
//
function errorCB(err) {
    console.log("Error processing SQL: "+err.code);
}

// Transaction success callback
//
function successCB() {
    var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
}


// PhoneGap is ready
//
function onDeviceReady() {
    var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
    db.transaction(populateDB, errorCB, successCB);   
}


//<debug>
Ext.Loader.setPath({
    'Ext': 'sdk/src',
    'Sencha': 'app',
    'Sqlite': 'sqlite'
});
//</debug>

Ext.require('Sencha.util.InitSQLite');

Ext.apply(Ext.util.Format, {
	defaultDateFormat: 'd/m/Y'
});

Ext.application({
    // Rename this
    name: 'Sencha',
   

     

    // Not sure if it is best practice to have all these here?
    controllers: ['DictionarySourceController', 'DictionaryDetailController'],

    views: ['DictionaryContainerList', 'DictionaryFormView', 'DictionarySourceListView', 'DictionarySourceNavigationView', 'DictionaryTargetDetailView', 'PhraseContainerList', 'PhraseListView', 'TabView', 'SoundRecorder', 'DicMetadataFormView', 'CollectorFormView', 'SpeakerFormView'   ],

    stores: ['DictionarySources', 'DictionaryTargets', 'Phrases', 'Collectors', 'Speakers'],
    models: ['DictionarySource', 'DictionaryTarget', 'Phrase', 'Collector', 'Speaker'],
    
    onReady: function() {
        onDeviceReady();
    	Sencha.util.InitSQLite.initDb();
    },
                    
    init: function() {

    	
    },            
    
    launch: function() {
        Ext.Viewport.add({
            xtype: 'tabview'
        });
    },
    
    onUpdated: function() {
        Ext.Msg.confirm(
            "Application Update",
            "This application has just successfully been updated to the latest version. Reload now?",
            function() {
                window.location.reload();
            }
        );
    }
    
});